快速上手
基本概念
quick start: https://guides.rubyonrails.org/getting_started.html
-
Route(路由):rules written in a Ruby DSL (Domain-Specific Language).
-
Controller(控制器):Ruby classes, and their public methods are actions(动作)
-
Views(视图):templates, usually written in a mixture of HTML and Ruby
- 数据是在控制器中获取的,而不是在视图中。视图只是把数据显示出来
创建新项目
Installing Rails
gem install rails
新建一个应用(名为 blog)
rails new blog
运行项目
bin/rails server
项目结构
File/Folder | Purpose |
---|---|
app/ | Contains the controllers, models, views, helpers, mailers, channels, jobs, and assets for your application. You'll focus on this folder for the remainder of this guide. |
bin/ | Contains the rails script that starts your app and can contain other scripts you use to set up, update, deploy, or run your application. |
config/ | Contains configuration for your application's routes, database, and more. This is covered in more detail in Configuring Rails Applications. |
config.ru | Rack configuration for Rack-based servers used to start the application. For more information about Rack, see the Rack website. |
db/ | Contains your current database schema, as well as the database migrations. |
Dockerfile | Configuration file for Docker. |
Gemfile Gemfile.lock | These files allow you to specify what gem dependencies are needed for your Rails application. These files are used by the Bundler gem. For more information about Bundler, see the Bundler website. |
lib/ | Extended modules for your application. |
log/ | Application log files. |
public/ | Contains static files and compiled assets. When your app is running, this directory will be exposed as-is. |
Rakefile | This file locates and loads tasks that can be run from the command line. The task definitions are defined throughout the components of Rails. Rather than changing Rakefile , you should add your own tasks by adding files to the lib/tasks directory of your application. |
README.md | This is a brief instruction manual for your application. You should edit this file to tell others what your application does, how to set it up, and so on. |
storage/ | Active Storage files for Disk Service. This is covered in Active Storage Overview. |
test/ | Unit tests, fixtures, and other test apparatus. These are covered in Testing Rails Applications. |
tmp/ | Temporary files (like cache and pid files). |
vendor/ | A place for all third-party code. In a typical Rails application this includes vendored gems. |
.dockerignore | This file tells Docker which files it should not copy into the container. |
.gitattributes | This file defines metadata for specific paths in a git repository. This metadata can be used by git and other tools to enhance their behavior. See the gitattributes documentation for more information. |
.github/ | Contains GitHub specific files. |
.gitignore | This file tells git which files (or patterns) it should ignore. See GitHub - Ignoring files for more information about ignoring files. |
.rubocop.yml | This file contains the configuration for RuboCop. |
.ruby-version | This file contains the default Ruby version. |
Route
Let's start by adding a route to our routes file, config/routes.rb
, at the top of the Rails.application.routes.draw
block:
# config/routes.rb
Rails.application.routes.draw do
get "/articles", to: "articles#index"
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
The route above declares that GET /articles
请求会被匹配到 ArticlesController
控制器的 index
动作上
运行 controller generator (with the --skip-routes
option 因为已经有了 route 定义),来创建 ArticlesController
bin/rails generate controller Articles index --skip-routes
Rails will create several files for you:
create app/controllers/articles_controller.rb
invoke erb
create app/views/articles
create app/views/articles/index.html.erb
invoke test_unit
create test/controllers/articles_controller_test.rb
invoke helper
create app/helpers/articles_helper.rb
invoke test_unit
其中 controller file 是最重要的, app/controllers/articles_controller.rb
. Let's take a look at it:
class ArticlesController < ApplicationController
def index
end
end
index
action 暂时没有内容. 当一个 action 没有明确地渲染一个 view (或 trigger an HTTP response) 时,Rails 会自动根据控制器和动作的名字而渲染一个默认的 view. 约定优于配置!(Convention Over Configuration)
Views 集中在 app/views
目录中. 故 index
action 会默认渲染 app/views/articles/index.html.erb
打开 app/views/articles/index.html.erb
,将内容替换为:
<h1>Hello, Rails!</h1>
运行服务:
bin/rails server